package com.ppx.life.service.impl;

import com.ppx.life.common.exception.ServiceException;
import com.ppx.life.dao.Bill;
import com.ppx.life.mapping.BillMapper;
import com.ppx.life.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BillServiceImpl implements BillService{

    @Autowired
    BillMapper billMapper;

    @Override
    public int add(List<Bill> bills,Integer user_id) {
        //1.判断参数是否合法
        if(bills==null) throw  new  IllegalArgumentException("参数异常");
        if(bills.get(0)==null) throw  new  IllegalArgumentException("参数异常");

        //2.执行bill表的添加操作
        int rows=-1;
        try {
            rows = billMapper.addBills(bills,user_id);
        }catch (Exception e){
            e.printStackTrace();
            throw  new ServiceException("系统错误！添加失败");
        }
        //3.执行bills表的添加操作
        StringBuilder names=null;
        String date = bills.get(0).getDate().substring(0,10);
        System.out.println("billServiceImpl.add   添加总记录的时间为："+date);
        double total_price=0;
        int userId = bills.get(0).getUserId();
        System.out.println("billServiceImpl.add   添加总记录的user_id为："+userId);
        //3.1先查询该日期的记录是否已创建
        if(billMapper.selectDataFromBills(date)==0){
            //如果没有创建则新建
            for (int i = 0; i < bills.size(); i++) {
                names.append(bills.get(0).getCommodityName());
                total_price+=
                        (bills.get(0).getUnitPrice()*
                                bills.get(0).getCommodityWeight());
            }
            System.out.println("billServiceImpl.add   添加总记录的total_price为："+total_price);
            try {
                billMapper.insertBills(date,names.toString(),total_price,userId);
            }catch (Exception e){
                e.printStackTrace();
                throw  new ServiceException("系统错误！添加失败");
            }
        }else{
            //如果已创建，则修改
            Map<String ,Object> map = billMapper.selectBillsByDate(date);
            String names01 = (String) map.get("commodity_names");
            Double total_price01 = (Double)map.get("total_price");
            StringBuilder name02 = null;
            name02.append(names01);
            name02.append(names.toString());
            total_price += total_price01;
            try {
                billMapper.insertBills(date,name02.toString(),total_price,userId);
            }catch (Exception e){
                e.printStackTrace();
                throw  new ServiceException("系统错误！添加失败");
            }
        }
        return rows;
    }

    @Override
    public List<Map<String, Object>> selectBillsList(Integer user_id,Integer indexPage) {
        //1.判断参数是否合法
        if(user_id==null){
            if(user_id==null) throw  new  IllegalArgumentException("参数异常");
        }
        //2.根据用户id查询出一共记录了多少天的商品
        int pageSize = 3;
        List<Map<String,Object>> list = billMapper.selectBillsList(user_id,indexPage,pageSize);
        return list;
    }

    @Override
    public List<Map<String, Object>> selectBillListByDate(String date) {
        //1.判断参数是否合法
        if(date==null){
            throw  new  IllegalArgumentException("参数异常");
        }
        //2.查询
        List<Map<String ,Object>> list;
        try {
            list = billMapper.selectBillListByDate(date);
        }catch (Exception e){
            e.printStackTrace();
            throw  new ServiceException("系统错误！添加失败");
        }
        return list;
    }

}
